<?php
require 'functions.php';
require '../data/data.php';

// 设定响应头信息
header('Content-Type:text/html;Charset=utf-8');

// 只支持POST请求，否则返回405错误
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
   header('HTTP/1.1 405 Method Not Allowed');
   return;
}

// 取出用户输入数据
$email = input('email');
$username = input('username', 'youke');
$gender = input('gender', 1);
$coursesA = input('course', []);
$courses = implode(',', $coursesA);
$birth = input('birth', '');
$attr = input('attr', 1);
$describ = input('describ', '');

$id = input('id', -1);

// 验证邮箱
if (empty($email)) {
   error('邮箱不能为空哦', '../user_info.php?op=1');
   return;
}
if(! filter_var($email, FILTER_VALIDATE_EMAIL)) {
   error('请输入正确的邮箱', '../user_info.php?op=1');
   return;
}

$sql = 'update users set email=?,user_name=?,gender=?,cuorses=?,birthday=?,attr=?,introducion=? where id=?';
$args = [$email, $username, $gender, $courses, $birth, $attr, $describ, $id];
if (execute($sql, $args)) {
   redirect('修改用户信息成功');
} else {
   error('修改用户信息失败', '../user_info.php?op=2&id=' . $id);
}